//
//  chathandle.m
//  Final
//
//  Created by lanou3g on 15/12/4.
//  Copyright © 2015年 皮比诺. All rights reserved.
//

#import "chathandle.h"

@implementation chathandle
+(chathandle *)sharehanle
{
    static dispatch_once_t onceToken;
    static chathandle *hanle = nil;
    dispatch_once(&onceToken, ^{
        hanle = [[chathandle alloc]init];
        
    });
    
    return hanle;
}


 static NSMutableArray *conversaatonArray = nil;

-(void)sendMessaWithClientId:(NSString *)mylientId toFriends:(NSArray *)toclients withText:(NSString *)content name:(NSString *)name
{
    self.client = [[AVIMClient alloc]init];
   
    
    
    
    [self.client openWithClientId:@"hahha" callback:^(BOOL succeeded, NSError *error) {
        [self.client createConversationWithName:nil clientIds:@[@"yyy"] callback:^(AVIMConversation *conversation, NSError *error) {
            
            [conversation sendMessage:[AVIMTextMessage messageWithText:@"天王盖地虎，宝塔镇河妖1" attributes:nil] callback:^(BOOL succeeded, NSError *error) {
                
                if (succeeded) {
                    NSLog(@"发送成功");
                }
                
            }];
            
            
            
        }];
        
    }];
    
}
//-(void)sendMessaWithClientId:(NSString *)mylientId toFriends:(NSArray *)toclients withText:(NSString *)content name:(NSString *)name
//{
//    self.client = [[AVIMClient alloc]init];
//    
//    [self.client openWithClientId:mylientId callback:^(BOOL succeeded, NSError *error) {
//        [self.client createConversationWithName:name clientIds:toclients callback:^(AVIMConversation *conversation, NSError *error) {
//            
//            [conversation sendMessage:[AVIMTextMessage messageWithText:content attributes:nil] callback:^(BOOL succeeded, NSError *error) {
//                
//                if (succeeded) {
//                    NSLog(@"发送成功");
//                }
//                
//            }];
//            
//            
//            
//        }];
//        
//    }];
//    
//}


-(void)receiveMessage{
    
    self.client = [[AVIMClient alloc]init];
    self.client.delegate = self;
    [self.client openWithClientId:@"yyy" callback:^(BOOL succeeded, NSError *error) {
        if (!error) {
            NSLog(@"收到消息");
        }
        
    }];
    
}

//收到消息后回调函数

- (void)conversation:(AVIMConversation *)conversation didReceiveTypedMessage:(AVIMTypedMessage *)message
{
    NSLog(@"%@",message.text);
    
}



//- (NSMutableArray *)queryConversationBymebers:(AVIMClient *)fromlient
//
//{
//    
//    __block NSMutableArray *array = nil;
//    
//    self.client = [[AVIMClient alloc]init];
//    
//    
//    
//    [self.client openWithClientId:@"hahha" callback:^(BOOL succeeded, NSError *error) {
//        
//        AVIMConversationQuery *query = [self.client conversationQuery];
//        
//        [query whereKey:@"m" containedIn:@[@"hahha", @"yyy"]];
//        //执行查询
//        query.limit = 30;
//        [query findConversationsWithCallback:^(NSArray *objects, NSError *error) {
//            
//            
//            NSLog(@" 找到%ld个对话", [objects count]);
//            array =  objects.mutableCopy;
//            
//            
//        }];
//        
//        
//    }];
//    
//    
//    return array;
//    
//}
//
//


- (void)queryConversationBymebers:(AVIMClient *)fromlient

{
    
        
     self.client = [[AVIMClient alloc]init];

        
        
        [self.client openWithClientId:@"hahha" callback:^(BOOL succeeded, NSError *error) {
            
            AVIMConversationQuery *query = [self.client conversationQuery];
            
            [query whereKey:@"m" containedIn:@[@"hahha", @"yyy"]];
            //执行查询
            query.limit = 30;
            [query findConversationsWithCallback:^(NSArray *objects, NSError *error) {
                if (!error) {
                    
                    
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"123" object:objects userInfo:nil];
                    
                    
                    NSLog(@" 找到%ld个对话", [objects count]);
                    conversaatonArray =  objects.mutableCopy;
                    NSLog(@"1");

                    
                }
                
                
                
                
            }];
            
            
        }];

        
        
        

    
    NSLog(@"2");
    
    
    
}



@end
